#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
void Pro_Wrte(int pfd)
{
    const char* str = "i am child,i write";
    char buffer[128];

    int cnt = 1;
    while(1)
    {
        snprintf(buffer,sizeof(buffer),"this is message : %s,pid:%d,cnt:%d",str,getpid(),cnt);
        write(pfd,buffer,strlen(buffer));//不需要向文件里面写\0
        cnt++;
        sleep(1);
    }
}
void Pro_Read(int pfd)
{
    char buffer[1024];
    while(1)
    {
        read(pfd,buffer,sizeof(buffer));
        sleep(1);
        printf("i am father,i get->>>>>> %s\n",buffer);

    }
}
int main()
{

    //1.父进程创建管道
    int pipefd[2];
    int n = pipe(pipefd);

    if(n < 0)return 1;
    //管道规定，0为读，1为写
    printf("pipefd[0]:%d,pipefd[1]:%d\n",pipefd[0],pipefd[1]);

    //2.创建子进程
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        close(pipefd[0]);
        Pro_Wrte(pipefd[1]);
        exit(0);
    }    
    //父进程
    close(pipefd[1]);
    Pro_Read(pipefd[0]);
    wait(NULL);

    return 0;
}